package com.cskaoyan.market.admin.service.impl;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import com.cskaoyan.market.admin.service.MarketAdminBrandService;
import com.cskaoyan.market.db.domain.MarketBrand;
import com.cskaoyan.market.db.domain.MarketBrandExample;
import com.cskaoyan.market.db.mapper.MarketBrandMapper;
import com.cskaoyan.market.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;

/**
 * @author BlackFlame33
 * @date 2023/03/19
 **/
public class MarketAdminBrandServiceImpl implements MarketAdminBrandService {
    @Override
    public List<MarketBrand> getBrandList(Integer page, Integer limit, String sort, String order, String id, String name) {
        SqlSession session = MyBatisUtil.getSession();
        MarketBrandMapper mapper = session.getMapper(MarketBrandMapper.class);
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();
        if (!StringUtils.isEmpty(id)) {
            criteria.andIdEqualTo(Integer.valueOf(id));
        }
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        marketBrandExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(page, limit);
        List<MarketBrand> marketBrands = mapper.selectByExample(marketBrandExample);
        session.commit();
        session.close();
        return marketBrands;
    }

    @Override
    public int createBrand(MarketBrand marketBrand) {
        SqlSession session = MyBatisUtil.getSession();
        MarketBrandMapper mapper = session.getMapper(MarketBrandMapper.class);
        int row = mapper.insertSelective(marketBrand);
        session.commit();
        session.close();
        return row;
    }

    @Override
    public MarketBrand getBrand(MarketBrand marketBrand) {
        SqlSession session = MyBatisUtil.getSession();
        MarketBrandMapper mapper = session.getMapper(MarketBrandMapper.class);
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        marketBrandExample.or().andNameEqualTo(marketBrand.getName()).andDescEqualTo(marketBrand.getDesc())
                .andFloorPriceEqualTo(marketBrand.getFloorPrice()).andPicUrlEqualTo(marketBrand.getPicUrl());
        MarketBrand result = mapper.selectOneByExample(marketBrandExample);
        session.commit();
        session.close();
        return result;
    }

    @Override
    public MarketBrand updateBrand(String name, String desc, String picUrl, Integer floorPrice, Integer id, String updateTime) {
        SqlSession session = MyBatisUtil.getSession();
        MarketBrandMapper mapper = session.getMapper(MarketBrandMapper.class);

        MarketBrand marketBrand = mapper.selectByPrimaryKey(id);
        marketBrand.setName(name);
        marketBrand.setDesc(desc);
        marketBrand.setPicUrl(picUrl);
        marketBrand.setFloorPrice(BigDecimal.valueOf(floorPrice));
        marketBrand.setUpdateTime(LocalDateTime.now());
        int i = mapper.updateByPrimaryKey(marketBrand);
        session.commit();
        session.close();
        return marketBrand;
    }

    @Override
    public void deleteBrand(Integer id) {
        SqlSession session = MyBatisUtil.getSession();
        MarketBrandMapper mapper = session.getMapper(MarketBrandMapper.class);
        mapper.deleteByPrimaryKey(id);
        session.commit();
        session.close();
    }
}